package serial

import (
	"time"

	"gitee.com/rw555/fdsmastergo/pkg/keypad"
)

// KeypadEvent 定义与现有keypad包兼容的事件类型
type KeypadEvent struct {
	Key       keypad.Key
	Type      KeypadEventType
	Timestamp time.Time
}

// KeypadEventType 按键事件类型
type KeypadEventType int

const (
	KeypadEventTypePress KeypadEventType = iota
	KeypadEventTypeLongPress
	KeypadEventTypeSuperLongPress
	KeypadEventTypeRelease
)

// String 返回事件类型的字符串表示
func (et KeypadEventType) String() string {
	switch et {
	case KeypadEventTypePress:
		return "short_press"
	case KeypadEventTypeLongPress:
		return "long_press"
	case KeypadEventTypeSuperLongPress:
		return "super_long_press"
	case KeypadEventTypeRelease:
		return "release"
	default:
		return "unknown"
	}
}

// parseKeyFromString 从字符串解析按键
func parseKeyFromString(keyStr string) keypad.Key {
	switch keyStr {
	case "F1":
		return keypad.KeyF1
	case "F2":
		return keypad.KeyF2
	case "F3":
		return keypad.KeyF3
	case "MENU", "Menu":
		return keypad.KeyMenu
	default:
		return keypad.KeyMenu // 默认返回菜单键
	}
}

// parseEventTypeFromString 从字符串解析事件类型
func parseEventTypeFromString(eventStr string) KeypadEventType {
	switch eventStr {
	case "short_press":
		return KeypadEventTypePress
	case "long_press":
		return KeypadEventTypeLongPress
	case "super_long_press":
		return KeypadEventTypeSuperLongPress
	case "release":
		return KeypadEventTypeRelease
	default:
		return KeypadEventTypePress // 默认返回按下事件
	}
}

// keypadStateFromEventType 从事件类型获取按键状态
func keypadStateFromEventType(eventType KeypadEventType) keypad.KeyState {
	switch eventType {
	case KeypadEventTypePress, KeypadEventTypeLongPress, KeypadEventTypeSuperLongPress:
		return keypad.KeyPressed
	case KeypadEventTypeRelease:
		return keypad.KeyReleased
	default:
		return keypad.KeyReleased
	}
}
